package com.example.capsule_chat0322.activity;

import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.NavigateAdapter;
import com.example.capsule_chat0322.configuration.Constant;
import com.example.capsule_chat0322.fragment.FriendListFragment;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.connect.ServerOperation;
import com.example.capsule_chat0322.fragment.ChatRoomListFragment;
import com.example.capsule_chat0322.fragment.PersonFragment;
import com.example.capsule_chat0322.global.LRUCache;
import com.google.android.material.tabs.TabLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/1 14:17
 */
public class MainActivity extends AppCompatActivity {


    private List<Fragment> mFragments;   //存放视图
    private ViewPager viewPager;
    private TabLayout mTabLayout;
    private List<String> mTitle;  //存放底部标题
    private final ServerOperation serverOperation = new ServerOperation();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity_main);
        initView();
        createFile();
    }


    private void initView() {

        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        int maxSize = maxMemory / 6;

        LRUCache<String, Bitmap> lruCache = new LRUCache<String, Bitmap>(maxSize) {
            @Override
            protected int sizeOf(@NonNull String key, @NonNull Bitmap bitmap) {
                return bitmap.getByteCount() / 1024;
            }
        };
        GlobalAssistMap.putObject("lruCache", lruCache);


        //String imei = DeviceIdUtil.getDeviceId(getApplicationContext());
        //imei = imei.replaceAll("-", "");

        GlobalAssistMap.putObject("imei", "4545adasdas21f546");
        GlobalAssistMap.putObject("context", getApplicationContext());

        serverOperation.connect(MainActivity.this);

        mTabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);


        mFragments = new ArrayList<>();
        mFragments.add(new ChatRoomListFragment());
        mFragments.add(new FriendListFragment());
        mFragments.add(new PersonFragment());

        mTitle = new ArrayList<>();
        mTitle.add("聊天室");
        mTitle.add("好友");
        mTitle.add("本人");

        //实例化适配器
        NavigateAdapter adapt = new NavigateAdapter(getSupportFragmentManager(), mFragments, mTitle);
        viewPager.setAdapter(adapt);
        mTabLayout.setupWithViewPager(viewPager);//给tab设置一个viewpager

        //viewpager的监听
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override  //选中
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        break;

                    case 1:
                        //Toast.makeText(MainActivity.this, "这是联系人", Toast.LENGTH_SHORT).show();
                        break;

                    case 2:
                        //Toast.makeText(MainActivity.this, "这是发现", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override   // //state的状态有三个，0表示什么都没做，1正在滑动，2滑动完毕
            public void onPageScrollStateChanged(int state) {

            }
        });


    }


    private void createFile() {

        GlobalAssistMap.putObject("fileSet", new HashSet<File>());

        File file = new File(Constant.IMAGE_PATH);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(Constant.FILE_PATH);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(Constant.VIDEO_PATH);
        if (!file.exists()) {
            file.mkdirs();
        }

    }


}
